#include"oj_model.hpp"
#include<stdlib.h>
#include<fstream>
#include<boost/filesystem/operations.hpp>
#include<boost/filesystem/path.hpp>
#include<boost/filesystem.hpp>

namespace fs = boost::filesystem;

/*查找文件后缀*/
void recursion(fs::path src_path, std::vector<std::string> &suffix_paths, const std::string& file_suffix) {
     fs::directory_iterator end;
     for (fs::directory_iterator dir(src_path); dir != end; dir++) {
        std::string fn = dir -> path().string();
        if (dir -> path().extension() == file_suffix) {
            std::string path = dir -> path().string();
            suffix_paths.push_back(path);
        }
     }
}
/*查找目录下的文件数量*/
int countfiles(fs::path src_path, const std::string& file_suffix) {
     fs::directory_iterator end;
     int num = 0;
     for (fs::directory_iterator dir(src_path); dir != end; dir++) {
         if (dir -> path().extension() == file_suffix) {
            num ++;
         }
     }
     return num;
}
int main() {
    
        try{
            MysqlObj& _mysqlObj  = MysqlObj::GetInstance();
            _mysqlObj.MysqlObjInitConnection();
            std::string problem_path = "./ProblemFile/ProblemDesc"; 
            int allnumber = countfiles(problem_path, ".desc");
            LOG(INFO, "allnumer is " + std::to_string(allnumber));
            int prevnumber = _mysqlObj.SelectCountAllFromProblem();
            LOG(INFO, "prevnumber is " + std::to_string(prevnumber));
            for (int i = 1; i <= allnumber; i++) {
                /*更新题目名称*/
                std::string problemName;
                FileUtil::ReadFile("./ProblemFile/ProblemName/" + std::to_string(i) + ".name", problemName);
                /*更新题目描述内容*/
                std::string problemDesc;
                FileUtil::ReadFile("./ProblemFile/ProblemDesc/" + std::to_string(i) + ".desc", problemDesc);
                problemDesc += "\n";
                LOG(INFO, "file context is:" + problemDesc);
                /*更新题目的难度*/
                std::string problemDifficulty;
                FileUtil::ReadFile("./ProblemFile/Difficulty/" + std::to_string(i) + ".diff", problemDifficulty);
                /*更新数据库中题目的测试集数量*/  
                std::vector<std::string> file_suffix;
                std::string src_path = "./DataTest/" + std::to_string(i); 
                recursion(src_path, file_suffix, ".in");
                LOG(INFO, "ProblemID: " + std::to_string(i) + "'s TestNum is" + std::to_string(file_suffix.size()) );
                if (prevnumber >= i) {
                       _mysqlObj.UpdateProblemTable(i, "ProblemName", problemName);
                       _mysqlObj.UpdateProblemTable(i, "Description", problemDesc);
                       _mysqlObj.UpdateProblemTable(i, "Difficulty", problemDifficulty);
                       _mysqlObj.UpdateProblemTable(i, "TestNum", std::to_string(file_suffix.size()));
                }
                else {
                       _mysqlObj.InsertProblemTable(i, problemName, problemDifficulty, problemDesc, std::to_string(file_suffix.size()));
                }
            }
            
        }
        catch(const std::string& str) {
            std::cout << "异常为:" << str << std::endl;
        }
        catch(...) {
            std::cout << "未知异常" << std::endl;
        }
    return 0;
}
